package com.cqs.leetcode.hash;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author lixiaowen
 * @create 2019-12-10
 */
public class LongestConsecutiveSequence128 {

    /**
     * hashMap + 线性表
     *
     * @param nums
     * @return
     */
    public int longestConsecutive(int[] nums) {
        if (nums == null) return 0;
        Set<Integer> set = Arrays.stream(nums).boxed().collect(Collectors.toSet());
        int longest = 0;
        for (Integer num : set) {
            //从连续序列的最左侧开始计数
            if (!set.contains(num - 1)) {
                Integer count = 0;
                while (set.contains(num + (++count)));
                if (count > longest) longest = count;
            }
        }
        return longest;
    }
}
